<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" %>
<%@ include file="/resources/modular/plugin/taglibs.jsp" %>
<!doctype html>
<head content="-我的">
    <%@ include file="/resources/modular/head/inside_head.jsp" %>
</head>
<body class="GC_Menu_7" name="GC_indvidual">

<%@ include file="/resources/modular/plugin/mainMenu.jsp" %>

<div class="page" name="GC_individual_placedevicemanage">

    <%@ include file="/resources/modular/plugin/mainTopMenu.jsp" %>

    <%@ include file="/resources/modular/plugin/individualMeun.jsp" %>

    <div class="page_right">
        <div class="my_placemanage_page">
            <p class="my_placemanage_pagep1">*系统初始密码默认为：123456<br>*修改密码，若不需要设置新密码，可直接恢复初始密码</p>
            <div class="my_placemanage_pagedv">
                <span>名称</span>
                <span>管理情况</span>
                <span>操作</span>
            </div>
            <ul class="my_placemanage_pageul1">

                <c:forEach items="${childuser}" var="user" varStatus="status">
                    <li>
                            <%-- 场地管理员信息 BEGIN --%>
                        <span class="deleteName">${user.username}</span>
                        <span class="GC_USREPLACE_PLACENAME_${user.userid}">
                            <c:if test="${fn:length(user.places) > 0}">
                                <c:forEach items="${user.places}" var="place" varStatus="placeStatus">
                                    ${place.placename}<c:if
                                        test="${fn:length(user.places)-placeStatus.index > 1}">、</c:if>
                                </c:forEach>
                            </c:if>
                        <c:if test="${fn:length(user.places) <= 0}">
                            尚未分配场地
                        </c:if>
                        </span>
                        <span class="my_placemanage_handlebtn">
                            <a class="my_placemanage_impower" href="javascript:;" GC_USERPLACE_userid="${user.userid}">场地分配</a>
                            <a class="my_placemanage_delete" href="javascript:;"
                               GC_USERPLACE_userid="${user.userid}">清除</a>
                        </span>
                            <%-- 场地管理员信息 END --%>
                            <%-- 授权管理 BEGIN --%>
                        <div class="my_childAccount_pointpage" style="background:#EEE; display: none;">
                            <div class="my_childAccount_pointpagedv">
                                    <%-- 已选择模块 BEGIN --%>
                                <div class="my_childAccount_pointpagedv_left">
                                    <p class="my_childAccount_pointpagep2">已分配场地名称</p>
                                    <dl class="my_childAccount_pointpagedv_leftul"
                                        GC_USERPLACE_userid="${user.userid}"><%-- 动态追加内容 --%>
                                        <c:forEach items="${user.places}" var="place" varStatus="placeStatus">
                                            <dd>
                                                <p class="my_childAccount_pointpagedv_p">
                                                    <span>${place.placename}</span>
                                                    <a href="javascript:;"
                                                       GC_USERPLACE_placeid="${place.placeid}">删除</a>
                                                </p>
                                                    <%--
                                                    <ul class="my_childAccount_pointpagedv_leftul_ul">
                                                        <li><span>查看账目</span><a href="javascript:;">删除</a></li>
                                                        <li><span>查看明细</span><a href="javascript:;">删除</a></li>
                                                    </ul>
                                                    --%>
                                            </dd>
                                        </c:forEach>
                                    </dl>
                                </div>
                                    <%-- 已选择模块 END --%>

                                    <%-- 可选择模块 BEGIN --%>
                                <div class="my_childAccount_pointpagedv_right">
                                    <p class="my_childAccount_pointpagep2">可分配场地名称</p>
                                    <dl class="my_childAccount_pointpagedv_rightul"
                                        GC_USERPLACE_userid="${user.userid}">
                                            <%-- 动态追加内容 --%>
                                    </dl>
                                </div>
                                    <%-- 可选择模块 END --%>
                                <p class="my_placemanage_pointpagep3">
                                    <a class="my_placemanage_addchildbtn1" href="javascript:;"
                                       GC_USERPLACE_userid="${user.userid}">确认</a>
                                    <a class="my_placemanage_addchildbtn2" href="javascript:;">取消</a></p>
                            </div>
                        </div>
                            <%-- 授权管理 END --%>
                    </li>
                </c:forEach>
            </ul>

        </div>

    </div>

    <!-- 删除账户 提示弹窗 -->
    <div class="pointpage_dv3" style="display: none;">
        <div class="pointpage3">
            <p>是否清除“<span class="my_placemanage_childname">李四</span>”已管理的场地</p>
            <p><a class="my_placemanage_deletebtn1" href="javascript:;">确认</a> <a
                    class="my_placemanage_deletebtn2" href="javascript:;">取消</a></p>
        </div>
    </div>
    <!-- 删除账户 提示弹窗 end -->

    <%--  权限修改成功 提示弹窗 --%>
    <div class="pointpage_dv4" style="display: none;">
        <div class="pointpage1">
            <p class="pointpage_beginpsd"></p>
            <p><a class="my_placemanage_confirmbtn1" href="javascript:;">确认</a></p>
        </div>
    </div>
    <%--  权限修改成功 提示弹窗 end --%>
</div>
</body>
</html>
<%@ include file="/resources/modular/foot/inside_footer.jsp" %>
<script type="text/javascript">

    $(".my_placemanage_deletebtn1").click(function () {
        var userid = $(this).attr("GC_USERPLACE_userid");

        if (typeof(userid) == "undefined" || userid.length <= 0) {
            alert("非法操作");
        } else {
            $.ajax({
                url: '${basePath}v2/individual/clearuserplaces',
                type: 'POST',
                data: {
                    "user": userid
                },
                traditional: true,
                error: function (response) {
                    $errorMsg(response);
                },
                success: function (ret) {
                    if (ret.result) {

                        $(".pointpage_beginpsd").text("场地清除成功");
                        $(".pointpage_dv4").stop().show();
                        $(".my_placemanage_confirmbtn1").on("click", function () {
                            location.reload(true);
                        });
                    } else {
                        alert(ret.info);
                    }
                }
            });
        }
    });

    <%-- 分配用户场地 --%>
    $(".my_childAccount_addchildbtn1, .my_placemanage_addchildbtn1").on("click", function () {
        var userid = $(this).attr("GC_USERPLACE_userid");
        if (typeof(userid) == "undefined" || userid.length <= 0) {
            alert("非法操作");
        } else {

            var userplaces = new Array();
            var placename = new Array();
            var GC_USERPLACE_places = $(".my_childAccount_pointpagedv_leftul[GC_USERPLACE_userid='" + userid + "']").children('dd');
            $.each(GC_USERPLACE_places, function (i, placeid) {
                userplaces[i] = parseInt($(this).find("a").attr("GC_USERPLACE_placeid"));
                placename[i] = $(this).find("span").text();
            });

            if (typeof(userplaces) == "undefined" || userplaces.length <= 0) {
                alert("您尚未选择场地");
                $(".my_placemanage_impower[GC_USERPLACE_userid='" + userid + "']").css("background", "#FFF").closest("li").find(".my_childAccount_pointpage").hide();
            } else {
                $.ajax({
                    url: '${basePath}v2/individual/setuserplaces',
                    type: 'POST',
                    data: {
                        "user": userid,
                        "place": userplaces
                    },
                    traditional: true,
                    error: function (response) {
                        $errorMsg(response);
                    },
                    success: function (ret) {
                        if (ret.result) {
                            $(".GC_USREPLACE_PLACENAME_" + userid).text("");
                            $.each(placename, function (i, place) {
                                var pname = place;
                                if (i != 0) {
                                    pname = "、" + pname;
                                }
                                $(".GC_USREPLACE_PLACENAME_" + userid).append(pname);
                            });

                            $(".my_placemanage_impower[GC_USERPLACE_userid='" + userid + "']").css("background", "#FFF").closest("li").find(".my_childAccount_pointpage").hide();
                            $(".pointpage_beginpsd").text("分配成功");
                            $(".pointpage_dv4").stop().show();
                            $(".my_placemanage_confirmbtn1").on("click", function () {
                                $(".pointpage_dv4").stop().hide();
                            });
                        } else {
                            alert(ret.info);
                        }
                    }
                });
            }
        }
    });


    <%-- 获取用户下的所有场地 --%>
    function getuserplaces(userid) {
        $.ajax({
            url: '${basePath}v2/individual/getuserplaces',
            type: 'GET',
            error: function (response) {
                $errorMsg(response);
            },
            success: function (ret) {
                if (ret.result) {
                    if (typeof(ret.data) == "undefined" || ret.data.length <= 0) {
                        alert("您尚未拥有场地");
                        $(".my_placemanage_impower[GC_USERPLACE_userid='" + userid + "']").css("background", "#FFF").closest("li").find(".my_childAccount_pointpage").hide();
                    } else {
                        <%-- 添加用户下的所有场地 --%>
                        $(".my_childAccount_pointpagedv_rightul[GC_USERPLACE_userid='" + userid + "']").children('dd').remove();
                        $.each(ret.data, function (index, item) {
                            var palcehtml = '<dd GC_USERPLACE_PLACE_DDID="' + item.placeid + '" ><p class="my_childAccount_pointpagedv_p"><span>' +
                                    item.placename + '</span><a href="javascript:;" GC_USERPLACE_placeid="' + item.placeid + '">添加</a></p></dd>';
                            $(".my_childAccount_pointpagedv_rightul[GC_USERPLACE_userid='" + userid + "']").append(palcehtml);

                        });
                        <%-- 移除用户已经分配的场地 --%>
                        var GC_USERPLACE_places = $(".my_childAccount_pointpagedv_leftul[GC_USERPLACE_userid='" + userid + "']").children('dd');
                        $.each(GC_USERPLACE_places, function (i, placeid) {
                            $("dd[GC_USERPLACE_PLACE_DDID='" + parseInt($(this).find("a").attr("GC_USERPLACE_placeid")) + "']").remove();
                        });
                        equipment_parameterDelete();
                        equipment_parameterAdd();
                    }
                } else {
                    alert(ret.info);
                }
            }
        });
    }


    //授权隐藏、显示
    $(".my_placemanage_impower").on("click", function () {
        if ($(this).parent().parent().find("div.my_childAccount_pointpage").is(":hidden")) {

            getuserplaces($(this).attr("GC_USERPLACE_userid"));
            $(this).parent().parent().find("div.my_childAccount_pointpage").stop().show().siblings("div").hide();
            $(this).css("background", "#EEE").siblings().css("background", "#FFF");
        } else {
            $(this).parent().parent().find("div.my_childAccount_pointpage").stop().hide();
            $(this).css("background", "#FFF");
        }
    });

    //场地授权  取消
    $(".my_placemanage_addchildbtn2").on("click", function () {
        $(this).parents(".my_childAccount_pointpage").stop().hide();
        $(this).closest("li").find(".my_placemanage_impower").css("background", "#FFF");
    });


    //增加参数组  左右添加、删除事件
    //左边 删除
    function equipment_parameterDelete() {
        $(".my_childAccount_pointpagedv_leftul dd a").unbind("click");
        $(".my_childAccount_pointpagedv_leftul dd a").on("click", function () {
            var equipment_parameter_html = $(this).parent().parent().html();
            $(this).parents(".my_childAccount_pointpagedv").find(".my_childAccount_pointpagedv_rightul").append("<dd>" + equipment_parameter_html + "</dd>");
            $(".my_childAccount_pointpagedv_rightul dd a").text("添加");
            $(this).parent().parent().remove();
            equipment_parameter_html = null;
            equipment_parameterDelete();
            equipment_parameterAdd();
        });
    }

    //右边 添加
    function equipment_parameterAdd() {
        $(".my_childAccount_pointpagedv_rightul dd a").unbind("click");
        $(".my_childAccount_pointpagedv_rightul dd a").on("click", function () {
            var equipment_parameter_html = $(this).parent().parent().html();
            $(this).parents(".my_childAccount_pointpagedv").find(".my_childAccount_pointpagedv_leftul").append("<dd>" + equipment_parameter_html + "</dd>");
            $(".my_childAccount_pointpagedv_leftul dd a").text("删除");
            $(this).parent().parent().remove();
            equipment_parameter_html = null;
            equipment_parameterDelete();
            equipment_parameterAdd();
        });
    }
    var deletaText = "";
    var GC_USERPLACE_userid = "";
    $(".my_placemanage_delete").on("click", function () {
        deletaText = $(this).parent().parent().find(".deleteName").text();
        GC_USERPLACE_userid = $(this).attr("GC_USERPLACE_userid");
        $(".my_placemanage_deletebtn1").attr("GC_USERPLACE_userid", GC_USERPLACE_userid);
        $(this).parents(".page").find(".pointpage_dv3").stop().show().find(".my_placemanage_childname").text(deletaText);
    });
    //删除 取消
    $(".my_placemanage_deletebtn2").on("click", function () {
        $(this).parents(".pointpage_dv3").stop().hide();
    });
    //删除 确认
    $(".my_placemanage_deletebtn1").on("click", function () {
        $(this).parents(".pointpage_dv3").stop().hide();

        $(this).parents(".page").find(".deleteName").find(":contains(" + deletaText + ")").parent().remove();
    });

</script>